package com.hu.zhcc.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.hu.zhcc.shiro.entity.bo.CheckItem;
import com.hu.zhcc.shiro.entity.bo.Lixueseng520.LxsDiet;
import com.hu.zhcc.shiro.entity.bo.Lixueseng520.LxsExercise;
import com.hu.zhcc.shiro.entity.bo.Template;
import com.hu.zhcc.shiro.entity.bo.Therapy;
import com.hu.zhcc.shiro.entity.vo.TherapyVO;
import com.hu.zhcc.shiro.service.TemplateService;
import com.hu.zhcc.shiro.service.TherapyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 干预方案
 */

@RestController
@CrossOrigin
@RequestMapping("/therapy")
public class TherapyController {
    @Autowired
    private TherapyService therapyService;
    @Autowired
    private TemplateService templateService;

    @GetMapping("")
    public PageInfo<Therapy> homeTherapy(TherapyVO therapyVO){
        PageInfo<Therapy> therapyPageInfo = therapyService.pageQuery(therapyVO);
        return therapyPageInfo;
    }

    @PostMapping("")
    public ResponseEntity<?> saveCheckitem(@RequestBody Therapy therapy) {
        return therapyService.AddTherapy(therapy) ? ResponseEntity.noContent().build() :
                ResponseEntity.notFound().build();
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTherapy(@PathVariable("id") Short id) {
        return therapyService.removeTherapy(id) ? ResponseEntity.noContent().build() :
                ResponseEntity.notFound().build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateRouter(@PathVariable("id") int id, @RequestBody Therapy therapy) {
        List<LxsDiet> lxsDiet = therapy.getLxsDiet();
        List<LxsExercise> lxsExercises = therapy.getLxsExercise();
        Integer[] lxsDietIds=new Integer[lxsDiet.size()];
        for (int i=0;i<lxsDiet.size();i++) {
            lxsDietIds[i] = lxsDiet.get(i).getId();
        }
        Integer[] lxsExercisesIds=new Integer[lxsExercises.size()];
        for (int i=0;i<lxsExercises.size();i++) {
            lxsExercisesIds[i] = lxsExercises.get(i).getId();
        }
        return therapyService.updateTherapy(therapy,lxsDietIds,lxsExercisesIds) ? ResponseEntity.noContent().build() :
                ResponseEntity.notFound().build();
    }
    @PutMapping("")
    public List<Template> find() {
        return  therapyService.find();
    }
    @GetMapping("/{id}")
    public Template findTemplate(@PathVariable("id") Short id) {
        return templateService.findByid(id);
    }
    @GetMapping("/diet/{id}")
    public List<LxsDiet> findLxsDietbyid(@PathVariable("id") String  id) {
        return therapyService.finddietbyid(id);
    }
    @GetMapping("/exercise/{id}")
    public List<LxsExercise> findExercisebyid(@PathVariable("id") String id) {
        return therapyService.findExercisebyid(id);
    }
}
